package com.lin.activity;


import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.lin.utils.Utils;
import com.lin.views.NiceGirlViewPager;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.bean.FriendsShareBean;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;


import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class FullScreenViewActivity extends Activity implements OnClickListener{

	private RelativeLayout mRelativeLayout;
	int mCurPage = 1;
	String id;
	NiceGirlViewPager mNiceGirlViewPager;
	private ImageButton mImageDownloadButton;
	private FriendsShareBean mFriendsShareBean;
	ArrayList<FriendsShareBean> mArrayListFriendsShareBean;
	TextView mTextViewPage;
	ImageButton mImageShareButton;
	ImageButton mImageBackButton;
	int num;
	int mCurPageOfServerRequest = 1;
	int mCurPageOfGallery = 1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fullscreen_view);
		//		viewPager = (ViewPager) findViewById(R.id.pager);
		
		mArrayListFriendsShareBean = getIntent().getParcelableArrayListExtra("datalist");
		index = getIntent().getIntExtra("dataitem", 0);
		position = getIntent().getIntExtra("position", 0);
		mCurPageOfGallery = position;
		mRelativeLayout = (RelativeLayout) findViewById(R.id.fullscreen_relativelayout);
		mTextViewPage = (TextView) findViewById(R.id.activity_fullscreen_textview_page);
		mImageShareButton = (ImageButton) findViewById(R.id.fullscreen_share_button);
		mImageShareButton.setVisibility(View.GONE);
		mImageBackButton = (ImageButton) findViewById(R.id.fullscreen_button_back);
		mImageDownloadButton = (ImageButton) findViewById(R.id.fullscreen_download_button);
		mNiceGirlViewPager = new NiceGirlViewPager(this);
		mRelativeLayout.addView(mNiceGirlViewPager, new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
		
		mImageBackButton.setOnClickListener(this);
		mImageShareButton.setOnClickListener(this);
		mImageDownloadButton.setOnClickListener(this);
		
		Intent i = getIntent();
		mNiceGirlViewPager.setData(mArrayListFriendsShareBean.get(index));
		// displaying selected image first
		mNiceGirlViewPager.setCurrentItem(position);
		mNiceGirlViewPager.setOnPageChangeListener(mPageChangeListener);
//		if(mCurPageOfServerRequest == 2) //第一次加载
		mTextViewPage.setText((mCurPageOfGallery + 1) + "/" + mArrayListFriendsShareBean.get(index).getPhotoList().size());
	/*	if (isFromNiceGirlView) {
			new Thread(mRunnable).start();
		}else {
			mArrayListFriendsShareBean = DataProvider.mFriendsShareBeanList;
			for(int i = 0 ; i < mArrayListGalleryThemeModel.size() ; i++){
				if (mArrayListGalleryThemeModel.get(i).getImageUrl().equals(getIntent().getStringExtra("goalUrl"))) {
					mCurPageOfGallery = i + 1;
				}
				num = mArrayListGalleryThemeModel.size();
			}
			mHandler.sendEmptyMessage(1);
		}*/
	}

	/*Runnable mRunnable = new Runnable()  {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			ServeApi mServeApi = new ServeApi();
			mServeApi.setReqHost(DataProvider.baseUrl);
			mServeApi.setReqAct("getGalleryThemeImages");
			mServeApi.setReqParam("id", id);
			mServeApi.setReqParam("page", mCurPageOfServerRequest + "");
			mServeApi.setReqParam("pageSize", 20 + "");
			JSONObject result = mServeApi.reqToServe();
			try {
				if(result.getInt("status") == 1) {					
					JSONObject mJSONObjectData = new JSONObject(result.getString("data"));
					num = mJSONObjectData.getInt("num");
					JSONArray mJSONArray= mJSONObjectData.getJSONArray("galleryData");


					int mSize  = mJSONArray.length();
					int cpt = 0;
					boolean ismCurPageCounted = false;
					while (cpt < mSize) {
						JSONObject mJSONObject = mJSONArray.getJSONObject(cpt);
						String id = mJSONObject.getString("id");
						String image = mJSONObject.getString("images");
						String title = mJSONObject.getString("description");
						GalleryThemeModel mGalleryThemeModel = new GalleryThemeModel(id,title,image);	
						mArrayListGalleryThemeModel.add(mGalleryThemeModel);
						cpt ++;
						if(ismCurPageCounted == false) {
							mCurPageOfServerRequest ++;
							ismCurPageCounted = true;
						}
					}

					mHandler.sendEmptyMessage(1);
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	};

	Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			//			mListView.setData(mArrayListGalleryThemeModel);
			Intent i = getIntent();
			int position = i.getIntExtra("position", 0);
			mNiceGirlViewPager.setData(mArrayListGalleryThemeModel);
			// displaying selected image first
			mNiceGirlViewPager.setCurrentItem(mCurPageOfGallery - 1);
			mNiceGirlViewPager.setOnPageChangeListener(mPageChangeListener);
//			if(mCurPageOfServerRequest == 2) //第一次加载
			mTextViewPage.setText(mCurPageOfGallery + "/" + mArrayListGalleryThemeModel.size());
		}

	};*/

	private final ViewPager.SimpleOnPageChangeListener mPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {

		@Override
		public void onPageSelected(final int position) {
			mCurPageOfGallery = position + 1;
			mTextViewPage.setText(mCurPageOfGallery + "/" + mArrayListFriendsShareBean.get(index).getPhotoList().size());
			if(mCurPageOfGallery >= num) {
//				Toast.makeText(getApplicationContext(), "已经是最后一页", Toast.LENGTH_LONG).show();
			}
			
		/*	if(position + 1 == mArrayListFriendsShareBean.size() - 1) {
				new Thread(mRunnable).start();
			}*/
		}
	};
	private FriendsShareBean mFriendsShareBean2;
	private int index;
	private int position;


	protected void onTabChanged(final PagerAdapter adapter, final int oldPosition, final int newPosition) {

	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		if(view == mImageBackButton) {
			finish();
		}  else if(view == mImageDownloadButton) {
			try {
				Toast.makeText(getApplicationContext(), "已下载到pictures文件夹", Toast.LENGTH_LONG).show();
				URL mUrl = new URL(mArrayListFriendsShareBean.get(mCurPageOfGallery-1).getPhoto());
				Utils.saveFile(mUrl, new File(mUrl.toString()).getName(), getApplicationContext());
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else if(view == mImageShareButton) {
			mFriendsShareBean = mArrayListFriendsShareBean.get(mCurPageOfGallery-1);
//			showShare();
		}
	}
	
	/*
	private void showShare() {
		ShareSDK.initSDK(this);
		OnekeyShare oks = new OnekeyShare();
		
		// 分享时Notification的图标和文字
		oks.setNotification(R.drawable.logotop,
				getString(R.string.app_name));
		// title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
		if(mFriendsShareBean.getTitle() != null)
		oks.setTitle(mFriendsShareBean.getTitle());
		// titleUrl是标题的网络链接，仅在人人网和QQ空间使用
		if(mFriendsShareBean.getImageUrl() != null)
		oks.setTitleUrl(mFriendsShareBean.getImageUrl());
		// text是分享文本，所有平台都需要这个字段
//		oks.setText(mReplyBean.getReplyContent().replaceAll("<br>", "")
//				.replaceAll(" +", ""));
		// imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
//		 oks.setImagePath("/sdcard/girl.jpg");
		oks.setImageUrl(mFriendsShareBean.getImageUrl());
		// url仅在微信（包括好友和朋友圈）中使用
//		Log.i("tag", "----------------------url-----------" + mReplyBean.getPostUrl());
		oks.setUrl("http://kapai.com/index.html");
		// comment是我对这条分享的评论，仅在人人网和QQ空间使用
		oks.setComment("我是测试评论文本");
		// site是分享此内容的网站名称，仅在QQ空间使用
		oks.setSite(getString(R.string.app_name));
		// siteUrl是分享此内容的网站地址，仅在QQ空间使用
		oks.setSiteUrl("http://kapai.com/index.html");
//		oks.setDialogMode();
		oks.setSilent(false);
		// 启动分享GUI
		oks.show(this);
	}*/
}
